<?php
declare(strict_types=1);

namespace LowCode\Core;

use LowCode\Support\Fluent;
use LowCode\Traits\HasMake;

/**
 * @method static $this make(int $code, bool $success,string $message,mixed $data)
 */
class Response extends Fluent
{
    use HasMake;
    /**
     * @var string message|msg
     */
    protected string $messageField = "message";
    protected string $message = "";

    /**
     * @param int $code
     * @param bool $success
     * @param string $message
     * @param mixed $data
     */
    public function __construct(int $code, bool $success,string $message,mixed $data)
    {
        parent::__construct(compact("code","success","message","data"));
    }

    /**
     * 成功响应
     * @param string $message
     * @param mixed|null $data
     * @return static
     */
    public static function success(string $message, mixed $data = null): static
    {
        return self::make(0,true,$message,$data);
    }

    /**
     * 失败响应
     * @param string $message
     * @param mixed|null $data
     * @return static
     */
    public static function error(string $message, mixed $data = null): static
    {
        return self::make(1,false,$message,$data);
    }

    /**
     * code
     * @param int $code
     * @return $this
     */
    public function code(int $code): static
    {
        return $this->setAttribute("code",$code);
    }

    /**
     * 消息提示
     * @param string $message
     * @return $this
     */
    public function message(string $message): static
    {
        $this->message = $message;
        return $this->setAttribute($this->messageField,$message);
    }

    /**
     * 显示失败提示
     * @param string $field
     * @return $this
     */
    public function setMessageField(string $field = "message"|"msg"): static
    {
        $this->messageField = $field;
        $this->setAttribute($field, $this->message);
        return $this;
    }

    /**
     * @param mixed $data
     * @return $this
     */
    public function data(mixed $data): static
    {
        return $this->setAttribute("data",$data);
    }
}
